package com.leps.nq.bu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leps.nq.bi.mapper.CustBIMapper;
import com.leps.nq.bu.entity.BuPump;
import com.leps.nq.bu.entity.BuWell;
import com.leps.nq.bu.mapper.BuPumpMapper;
import com.leps.nq.bu.mapper.BuWellMapper;
import com.leps.nq.bu.service.IBuWellService;
import com.leps.nq.bu.vo.WellQueryModel;
import com.leps.nq.bu.vo.WellVo;
import com.leps.nq.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 井表 服务实现类
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@Service
public class BuWellServiceImpl extends ServiceImpl<BuWellMapper, BuWell> implements IBuWellService {
    @Autowired
    private BuWellMapper wellMapper;
    @Autowired
    private BuPumpMapper pumpMapper;
    @Autowired
    private CustBIMapper custBIMapper;

    @Override
    public PageVo<WellVo> pageByQueryModel(WellQueryModel queryModel) {
        Page<WellVo> page = Page.of(queryModel.getPageNum(), queryModel.getPageSize());
        page = this.wellMapper.selectByQueryModel(page, queryModel);
        return PageVo.of(page);
    }

    @Override
    public boolean save(BuWell well) {
        // 防止界面上没有选择客户、区块、平台、PLC
        Integer pumpId = well.getPumpId();
        if (ObjectUtil.isNull(well.getPlcId()) || ObjectUtil.isNull(well.getStationId()) || ObjectUtil.isNull(well.getAreaId()) || ObjectUtil.isNull((well.getCustId()))) {
            BuPump pump = this.pumpMapper.selectById(pumpId);
            well.setCustId(pump.getCustId());
            well.setAreaId(pump.getAreaId());
            well.setStationId(pump.getStationId());
            well.setPlcId(pump.getPlcId());
        }
        return super.save(well);
    }

    @Override
    public void updateRedundantField(BuWell well) {
        this.wellMapper.updateRedundantField(well);
    }

    @Override
    public List<WellVo> getWellsByStationId(Integer stationId) {
        return this.wellMapper.selectListByStationId(stationId);
    }

    @Override
    public List<WellVo> getWellsByCustIdAndStationId(int custId, Integer stationId) {
        return this.wellMapper.selectListByCustIdAndStationId(custId, stationId);
    }
}
